package cn.sh.library.mgts.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.impl.LiteralImpl;


public final class RDFUtils {
	public static List<Map<String, String>> transformListMap(
			List<Map<String, Object>> list) {
		List resultList = new ArrayList();
		if (list != null) {
			for (Map item : list) {
				resultList.add(transform(item));
			}
		}
		return resultList;
	}

	public static Map<String, String> transform(Map<String, Object> map) {
		if (map == null) {
			return null;
		}
		Map result = new LinkedHashMap();
		for (String key : map.keySet()) {
			result.put(key, toString(map.get(key)));
		}
		return result;
	}

	public static String toString(Object obj) {
		if (obj == null) {
			return "";
		}
		if ((obj instanceof Literal)) {
			Literal literal = (Literal) obj;
			return literal.getString();
		}
		return obj.toString();
	}

	public static String getLink(Model model, String shortLabel) {
		if (StringUtils.isEmpty(shortLabel)) {
			return "";
		}

		if (shortLabel.startsWith("http://")) {
			return shortLabel;
		}

		String[] temps = shortLabel.split(":");
		if (temps.length != 2)
			return "";
		String prefix = temps[0];
		String link = model.getNsPrefixURI(prefix);
		if (StringUtils.isEmpty(link)) {
			return "";
		}
		return link + temps[1];
	}

	public static String toShortLabel(Model model, Object obj) {
		if (obj == null)
			return "";
		String shortLabel = "";

		if ((obj instanceof Resource)) {
			String label = "";
			String uri = "";
			if (obj.toString().contains("#")) {
				uri = StringUtils.substringBeforeLast(obj.toString(), "#")
						+ "#";
				label = StringUtils.substringAfterLast(obj.toString(), "#");
			} else {
				uri = StringUtils.substringBeforeLast(obj.toString(), "/")
						+ "/";
				label = StringUtils.substringAfterLast(obj.toString(), "/");
			}

			String prefix = model.getNsURIPrefix(uri);

			if (StringUtils.isNotBlank(prefix))
				shortLabel = prefix + ":" + label;
			else {
				shortLabel = obj.toString();
			}
		}

		return shortLabel;
	}

	public static String getLabel(Object obj) {
		if (obj == null)
			return "";

		if (obj.toString().contains("#")) {
			return StringUtils.substringAfterLast(obj.toString(), "#");
		}

		return StringUtils.substringAfterLast(obj.toString(), "/");
	}

	public static String getValue(String str) {
		if (str.contains("@")) {
			return str.split("@")[0];
		}

		if (str.contains("^^")) {
			return str.split("\\^\\^")[0];
		}

		return str;
	}

	public static Map<String, Object> getMap(ArrayList list, String[] args) {
		Map result = new HashMap();

		for (int i = 0; i < list.size(); i++) {
			String p = ((Map) list.get(i)).get("p").toString();
			p = p.substring(p.lastIndexOf("/") + 1);
			p = p.indexOf("#") != -1 ? p.substring(p.lastIndexOf("#") + 1) : p;

			Object o = ((Map) list.get(i)).get("o");
			Object obj = result.get(p);
			if (obj == null) {
				if (args.length == 2) {
					if (args[0].equals(p)) {
						if (StringUtils.isNotBlank(args[1]))
							result.put(args[1], getValue(o.toString()));
						else
							result.put(((LiteralImpl) o).getLanguage() + "_"
									+ p, getValue(o.toString()));
					} else
						result.put(p, o.toString());
				} else {
					result.put(p, o.toString());
				}
			} else if ((obj instanceof List)) {
				((List) obj).add(o.toString());
			} else {
				List sList = new ArrayList();
				sList.add(obj.toString());
				sList.add(getValue(o.toString()));
				result.put(p, sList);
			}

		}

		return result;
	}

	public static Map<String, Object> getMap(Model model, ArrayList list) {
		Map result = new HashMap();

		for (int i = 0; i < list.size(); i++) {
			String p = ((Map) list.get(i)).get("p").toString();
			String prefix_uri = "";
			String node = "";
			if (p.contains("#")) {
				prefix_uri = p.substring(0, p.lastIndexOf("#") + 1);
				node = p.substring(p.lastIndexOf("#") + 1);
			} else {
				prefix_uri = p.substring(0, p.lastIndexOf("/") + 1);
				node = p.substring(p.lastIndexOf("/") + 1);
			}

			p = model.getNsURIPrefix(prefix_uri) + "_" + node;

			String o = getValue(((Map) list.get(i)).get("o").toString());
			Object obj = result.get(p);
			if (obj == null) {
				result.put(p, o);
			} else if ((obj instanceof List)) {
				((List) obj).add(o);
			} else {
				List sList = new ArrayList();
				sList.add(obj.toString());
				sList.add(o);
				result.put(p, sList);
			}

		}

		return result;
	}
}
